package com.caleble.sample.dao.ibatis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import com.caleble.sample.domain.Sequence;
import com.ibatis.sqlmap.client.SqlMapClient;

@Component
@Scope("prototype")
public class OracleSequenceDao extends SqlMapClientDaoSupport {

	@Autowired
	public void setSuperSqlMapClient(SqlMapClient sqlMapClient) { // 自动注入SqlMapClient
		super.setSqlMapClient(sqlMapClient);
	}

	/**
	 * Get the next sequence using an Oracle thread-safe sequence
	 * 
	 * @param name
	 *            Name is the name of the oracle sequence.
	 * @return the next sequence
	 */
	public int getNextId(String name) throws DataAccessException {
		Sequence sequence = new Sequence();
		sequence.setName(name);
		sequence = (Sequence) getSqlMapClientTemplate().queryForObject("Sequence.oracleSequence", sequence);
		return sequence.getNextId();
	}
}
